import React, { useReducer } from 'react'
// useReducer(用来定义复杂类型的数据)

function reducer(state, { type}) {
    switch(type) {
        case "CHANGE":
            state.name = "李四"
            return { ...state }
    }
}

const initState = {
    name: "张三",
    age: 18
}

export default function About3() {
    // useReducer(reducer, initState)
    const [state, dispatch] = useReducer(reducer, initState)
    console.log(useReducer(reducer, initState))
    return (
        <div>
            <p>
                姓名: { state.name }
            </p>
            <p>
                年龄: { state.age }
            </p>
            <div>
                <button onClick={() => {
                    dispatch({ type: "CHANGE" })
                }}>修改姓名</button>
            </div>
        </div>
    )
}
